package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type LogMedRepository interface {
	Create(log *model.LogMed) error
	GetByID(id int64) (*model.LogMed, error)
	ListByApiCode(apiCode string) ([]model.LogMed, error)
	ListByHospital(hospitalCode string) ([]model.LogMed, error)
	Delete(id int64) error
}
type gormLogMedRepository struct {
	db *gorm.DB
}

func NewGormLogMedRepository(db *gorm.DB) LogMedRepository {
	return &gormLogMedRepository{db: db}
}

func (r *gormLogMedRepository) Create(log *model.LogMed) error {
	return r.db.Create(log).Error
}

func (r *gormLogMedRepository) GetByID(id int64) (*model.LogMed, error) {
	var log model.LogMed
	err := r.db.First(&log, id).Error
	return &log, err
}

func (r *gormLogMedRepository) ListByApiCode(apiCode string) ([]model.LogMed, error) {
	var logs []model.LogMed
	err := r.db.Where("api_code = ?", apiCode).Find(&logs).Error
	return logs, err
}

func (r *gormLogMedRepository) ListByHospital(hospitalCode string) ([]model.LogMed, error) {
	var logs []model.LogMed
	err := r.db.Where("hospital_code = ?", hospitalCode).Find(&logs).Error
	return logs, err
}

func (r *gormLogMedRepository) Delete(id int64) error {
	return r.db.Delete(&model.LogMed{}, id).Error
}
